import axios from "axios"
import store from "@/store"
import QS from "qs"

let instance,instance2;
if (process.env.NODE_ENV == 'production'){
    instance = axios.create({
      baseURL: "",
    })
      instance2 = axios.create({
      baseURL: "",
    })
}else{
    instance = axios.create({
      baseURL: "/api",
    })
      instance2 = axios.create({
      baseURL: "/api",
    })
}


instance.interceptors.request.use(
  function(config) {
    // console.log(config);
    
    if (config.method === "post") {
      config.data = QS.stringify(config.data)
    }

    let use = store.state.use

    // let IP = "123.9.191.96"

    // config.headers['WL-Proxy-Client-IP'] = `${IP}`

    if (use && use.token) {
      config.headers.token = `${use.token}`
      config.headers['Content-Type'] = "application/x-www-form-urlencoded/json"
    }
    return config
  },
  function(error) {
    return Promise.reject(error)
  }
)

instance.interceptors.response.use(
  function(response) {
    return response.data || response.data
  },
  function(error) {
    return Promise.reject(error)
  }
)

instance2.interceptors.request.use(
  function(config) {
    let use = store.state.use

    // let IP = window.localStorage.getItem("ip")

    // config.headers['WL-Proxy-Client-IP'] = `${IP}`

    if (use && use.token) {
      config.headers.token = `${use.token}`;
      config.headers['Content-Type'] = "application/x-www-form-urlencoded/json"
    }
    return config
  },
  function(error) {
    return Promise.reject(error)
  }
)

instance2.interceptors.response.use(
  function(response) {
    return response
  },
  function(error) {
    return Promise.reject(error)
  }
)
class https {
  async get(url, params) {
    return await instance.get(url, {
      params,
    })
  }

  async post(url, params) {
    return await instance.post(url, {
      params,
    })
  }
}
let http = new https()
export { http, instance2 }
